﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class FormationPanel : Page
    {
        public Dictionary<int, string> postions;
        public GameObject formationGroup;
        public GameObject formationItem;
        public GameObject actorScrollView;
        public GameObject actorGrid;
        public GameObject actorItem;

        public GameObject formationModelGroupPrefab;
        public FormationModelGroup formationModelGroup;

        public UIInput input;

        [HideInInspector]
        public Actors selectActor;
        public GameObject tipSpr;
        public List<Actors> currentPageActors;
        public static FormationPanel _instance;

        void Awake()
        {
            _instance = this;


        }
        /// <summary>
        /// 实例化玩家数据
        /// </summary>
        void InitDemoActor()
        {
            ActorManager.Instance.InitActor();
        }

        public void InitDemoPosition()
        {
            postions = new Dictionary<int, string>();
            postions.Add(1, "-226,64;0,0,2");
            postions.Add(2, "-22,44;0,0,2");
            postions.Add(3, "173,60;0,0,2");
            postions.Add(4, "376,40;0,0,2");
        }

        // Use this for initialization
        //void Start()
        //{

        //    InitPanel();
        //}

        protected override void DoOpen()
        {
            formationGroup.SetActive(true);
            InitDemoActor();
            InitDemoPosition();
            InitPanel();
        }



        /// <summary>
        /// 显示技能描述
        /// 暂时假数据，自己瞎写的
        /// </summary>
        /// <param name="actor"></param>
        public void ShowTip(Actors actor)
        {

            //Dict.DataModel.TableSkill skill = GetSkill(actor.heroid,(int)Battle.SkillType.Skill2);
            // Dict.DataModel.TableSkill skill2 = GetSkill(actor.heroid, (int)Battle.SkillType.Ultimate);
            tipSpr.SetActive(true);
            tipSpr.GetComponent<TipItem>().SetDes(Battle.SkillType.Skill2, "打狗棍法", "其实我是打猫的", Battle.SkillType.Ultimate, "打猫棍法", "其实我专门打狗");
        }
        /// <summary>
        /// 技能描述隐藏
        /// </summary>
        public void HideTip()
        {
            tipSpr.SetActive(false);
        }
        /// <summary>
        /// 获取技能信息
        /// </summary>
        /// <param name="heroId"></param>
        /// <param name="skillType"></param>
        /// <returns></returns>
        private Dict.DataModel.TableSkill GetSkill(string heroId, int skillType)
        {
            List<Dict.DataModel.TableSkill> skills = Dict.DictDataManager.Instance.tableSkillDao.CacheList;
            foreach (var item in skills)
            {
                if (item.SkillClass == skillType)
                {
                    return item;
                }

            }
            return null;
        }

        /// <summary>
        /// 初始化阵容信息面板
        /// </summary>
        private void InitPanel()
        {
            SaveBattleHero.Instance.ClearHeroCache();
            formationModelGroup = GameObject.Instantiate(formationModelGroupPrefab).GetComponent<FormationModelGroup>();

            currentPageActors = ActorManager.Instance.GetActor();

            InitMyActors(currentPageActors);
            InitFormation();
            actorGrid.GetComponent<UIGrid>().Reposition();
            actorScrollView.GetComponent<UIScrollView>().ResetPosition();

        }
        /// <summary>
        /// 初始化英雄
        /// </summary>
        /// <param name="currentPageActors"></param>
        private void InitMyActors(List<Actors> currentPageActors)
        {

            foreach (var item in currentPageActors)
            {

                GameObject go = Instantiate(actorItem) as GameObject;

                go.transform.parent = actorGrid.transform;

                go.transform.localScale = Vector3.one;

                go.SetActive(true);

                go.GetComponent<ActorItem>().Init(item);

            }
        }
        /// <summary>
        /// 左边头像上选择英雄
        /// </summary>
        /// <param name="actor"></param>
        public void SetSelectActor(Actors actor)
        {

            this.selectActor = actor;

            foreach (var item in actorGrid.GetComponent<UIGrid>().GetChildList())
            {
                item.gameObject.GetComponent<ActorItem>().SetState(false);
            }

            foreach (var item in formationModelGroup.allFormation)
            {


                if (actor != null)
                {

                    item.GetComponent<FormationItem>().ShowLight();


                }
                else
                {

                    item.GetComponent<FormationItem>().HideLight();
                }
            }
        }
        /// <summary>
        /// 删除面板上的英雄
        /// </summary>
        /// <param name="actor"></param>
        public void RemoveFormation(Actors actor)
        {


            foreach (var item in formationModelGroup.allFormation)
            {

                if (item.GetComponent<FormationItem>().formationItemModel != null && item.GetComponent<FormationItem>().formationItemModel.GetComponent<FormationItemModel>().actor == actor)
                {

                    item.GetComponent<FormationItem>().formationItemModel.GetComponent<FormationItemModel>().RemoveActorFromFormation();



                }
                else
                {

                }
            }
        }
        /// <summary>
        /// 初始化英雄阵容面板
        /// </summary>
        private void InitFormation()
        {

            formationModelGroup.allFormation = new List<GameObject>();
            foreach (var item in postions)
            {
                string[] str = item.Value.Split(';');
                GameObject go = Instantiate(formationItem) as GameObject;
                go.transform.parent = formationGroup.transform;
                go.transform.localScale = Vector3.one;
                go.SetActive(true);
                go.transform.localPosition = new Vector3(int.Parse(str[0].Split(',')[0]), int.Parse(str[0].Split(',')[1]), 0);

                go.GetComponent<FormationItem>().Init(item.Key, str[1]);
                formationModelGroup.allFormation.Add(go);

            }
        }

        /// <summary>
        /// 开始战斗
        /// </summary>
        public void FightBegin()
        {
            StaticData.isTestBattle = true;
            LoadingPanel.Instance.ShowLoading();
            _FightBegin();
            //BlackScreen.Instance.ShowBlack(_FightBegin);
        }

        void _FightBegin()
        {
            DestoryPanelObject();

            SaveBattleHero.Instance.GetBattleActorInfo();

            CameraTool.Instance.SwitchBattleCamera();
            int id = 1;
            int.TryParse(input.value, out id);

            StaticData.curBattleData.SetTestData(BattleType.PVE, id);

            Room.RoomManager.Switch(SceneType.BATTLE);

            Battle.BattleManager.Initialized();

            Battle.BattleManager.Instance.NewFight();
        }

        private void DestoryPanelObject()
        {


            foreach (var item in formationModelGroup.allFormation)
            {
                Destroy(item);
            }
            foreach (var item in actorGrid.GetComponent<UIGrid>().GetChildList())
            {

                Destroy(item.gameObject);
            }
            Destroy(formationModelGroup.gameObject);
            //Destroy(formationModelGroupPrefab.gameObject);
            postions.Clear();
            currentPageActors.Clear();
            formationModelGroup.cameraList.Clear();
            formationModelGroup.modelList.Clear();
            formationGroup.SetActive(false);
            gameObject.SetActive(false);

        }

        public override void Close()
        {
            if (formationModelGroup != null)
            {
                Destroy(formationModelGroup.gameObject);
            }
            base.Close();
        }

    }
}
